﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊排班管理
    /// </summary>
    public class OtpSchdManager : DomainService
    {
        private readonly IRepository<OtpSchdPlan, Guid> _optSchdPlanRepository;
        private readonly IRepository<OtpSchdRec, Guid> _optSchdRecRepository;
        private readonly IRepository<OtpSchdRecDetl, Guid> _clinicScheduleTimeRepository;
        private readonly IRepository<OtpRgstType, Guid> _optRgstTypeRepository;


        public OtpSchdManager(IRepository<OtpSchdPlan, Guid> optSchdPlanRepository
            , IRepository<OtpSchdRecDetl, Guid> clinicScheduleTimeRepository
            , IRepository<OtpSchdRec, Guid> optSchdRecRepository
            , IRepository<OtpRgstType, Guid> optRgstTypeRepository)
        {
            _optSchdPlanRepository = optSchdPlanRepository;
            _clinicScheduleTimeRepository = clinicScheduleTimeRepository;
            _optSchdRecRepository = optSchdRecRepository;
            _optRgstTypeRepository = optRgstTypeRepository;
        }

        public OtpSchdType CreateClinicSchedulePlan(OtpSchdType clinicSchedulePlan)
        {
            //clinicSchedulePlan.IsValid = true;
            //clinicSchedulePlan.Label = clinicSchedulePlan.BeginDate.ToString("yy-MM-dd") + " 至 " + clinicSchedulePlan.EndDate.ToString("yy-MM-dd");

            return clinicSchedulePlan;
        }

        /// <summary>
        /// 根据排班计划生成排班记录
        /// </summary>
        /// <param name="clinicSchedulePlanId"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task GenerateOtpSchdRec(Guid id)
        {
            var optSchdPlan = await _optSchdPlanRepository.GetAsync(id);
            var schdDate = optSchdPlan.Begndt;

            foreach (var optSchdPeriod in optSchdPlan.OtpSchdPeriods)
            {
                //门诊排班记录
                OtpSchdRec optSchdRec = new OtpSchdRec();
                optSchdRec.OtpSchdPlanId = optSchdPlan.Id;
                optSchdRec.OtpSchdPeriodId = optSchdPeriod.Id;

                optSchdRec.SchdDate = schdDate; //排班日期
                optSchdRec.SchdWeek = optSchdPeriod.Week;
                optSchdRec.DeptCodg = optSchdPlan.DeptCodg;
                optSchdRec.DeptName = optSchdPlan.DeptName;
                optSchdRec.DrCodg = optSchdPlan.DrCodg;
                optSchdRec.DrName = optSchdPlan.DrName;
                optSchdRec.DrProfttlCode = optSchdPlan.DrProfttlCode;
                optSchdRec.DrProfttlName = optSchdPlan.DrProfttlName;

                var optRgstType = await _optRgstTypeRepository.GetAsync(optSchdPlan.OtpRgstTypeId);
                //optSchdRec.Amt = optRgstType.Amt;
                optSchdRec.SchdStas = EnumSchdStas.出诊;
                optSchdRec.OtpRgstTypeId = optSchdPlan.OtpRgstTypeId;
                //optSchdRec.OtpRgstTypeCodg = optSchdPlan.OtpRgstTypeCodg;
                //optSchdRec.OtpRgstTypeName = optSchdPlan.OtpRgstTypeName;
                optSchdRec.Total = optSchdPeriod.AmPeriodCnt;
                optSchdRec.OverCnt = optSchdPeriod.AmPeriodCnt;
                optSchdRec.FollupFlag = optSchdPlan.FollupFlag;
                optSchdRec.MedrecFlag = optSchdPlan.MedrecFlag;
                optSchdRec.TrgFlag = optSchdPlan.TrgFlag;
                optSchdRec.OtpSchdRecDetls = new List<OtpSchdRecDetl>();

                OtpSchdRecDetl optSchdRecDetl = new OtpSchdRecDetl();
                optSchdRecDetl.SchdDate = schdDate;
                optSchdRecDetl.SchdBegntime = schdDate;
                optSchdRecDetl.SchdEndtime = schdDate;
                optSchdRecDetl.Total = optSchdPeriod.AmPeriodCnt;
                optSchdRecDetl.OverCnt = optSchdPeriod.AmPeriodCnt;
                optSchdRec.OtpSchdRecDetls.Add(optSchdRecDetl);

                if (optSchdPeriod.TimedivnFlag)
                {
                    
                }

                optSchdRec = await _optSchdRecRepository.InsertAsync(optSchdRec);
            }
        }
    }
}
